import random

import pygame
from pygame import *
import time


# 飞机类
# 属性
class Aircratf(object):
    def __init__(self, screen):
        # 2.1创建图片当做背景
        self.player = pygame.image.load('E:\pythonV1\python-script\item one\素材\自家飞机.png')
        # 定义飞机坐标
        self.x = 480 / 2 - 100 / 2
        self.y = 650

        # 飞机速度
        self.speed = 8
        # 记录当前窗口对象
        self.screen = screen
        # 装子弹的列表
        self.bullets = []

    def key_control(self):
        # 监听键盘事件
        key_pressed = pygame.key.get_pressed()

        if key_pressed[K_w] or key_pressed[K_UP]:
            self.y -= self.speed
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            self.y += self.speed
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            self.x -= self.speed
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            self.x += self.speed
        if key_pressed[K_SPACE]:
            # 按下空格发射子弹
            bullet = Bullet(self.screen, self.x, self.y)
            # 把子弹放到列表里
            self.bullets.append(bullet)

    def display(self):
        # 3.1将飞机图片贴在窗口
        self.screen.blit(self.player, (self.x, self.y))
        # 遍历所有子弹
        for bullet in self.bullets:
            """让子弹飞 修改子弹y坐标"""
            bullet.auto_move()
            # 子弹显示到窗口
            bullet.display()


# 敌机类
# 属性
class Enemyplane(object):
    def __init__(self, screen):
        # 2.1创建图片当做背景
        self.player = pygame.image.load('E:\pythonV1\python-script\item one\素材\敌机.png')
        # 定义飞机坐标
        self.x = 100
        self.y = 0

        # 飞机速度
        self.speed = 8
        # 记录当前窗口对象
        self.screen = screen
        # 装子弹的列表
        self.bullets = []
        # 敌机移动方向
        self.direction = 'right'

    def display(self):
        # 3.1将飞机图片贴在窗口
        self.screen.blit(self.player, (self.x, self.y))
        # # 遍历所有子弹
        for bullet in self.bullets:
            """让子弹飞 修改子弹y坐标"""
            bullet.auto_move()
            # 子弹显示到窗口
            bullet.display()

    def auto_move(self):
        if self.direction == 'right':
            self.x += self.speed
        elif self.direction == 'left':
            self.x -= self.speed
        if self.x > 480 - 76:
            self.direction = 'left'
        elif self.x < 0:
            self.direction = 'right'

    def auto_fire(self):
        """自动开火 创建子弹对象 添加进列表"""
        random_num = random.randint(1, 10)
        if random_num == 10:
            bullet = EnemyBullet(self.screen, self.x, self.y)
            self.bullets.append(bullet)


# 子弹类
# 属性
class Bullet():
    def __init__(self, screen, x, y):
        # 坐标
        self.x = x + 100 / 2 - 6 / 2
        self.y = y - 20
        # 图片
        self.image = pygame.image.load('E:\pythonV1\python-script\item one\素材\子弹.png')
        # 窗口
        self.screen = screen
        # 速度
        self.speed = 10

    def display(self):
        # 显示飞机子弹到窗口
        self.screen.blit(self.image, (self.x, self.y))

    def auto_move(self):
        """让子弹飞 修改子弹y坐标"""
        self.y -= self.speed


# 敌机子弹类
# 属性
class EnemyBullet():
    def __init__(self, screen, x, y):
        # 坐标
        self.x = x + 50 / 2 - 8 / 2
        self.y = y + 39
        # 图片
        self.image = pygame.image.load('E:\pythonV1\python-script\item one\素材\敌机子弹.png')
        # 窗口
        self.screen = screen
        # 速度
        self.speed = 10

    def display(self):
        # 显示飞机子弹到窗口
        self.screen.blit(self.image, (self.x, self.y))

    def auto_move(self):
        """让子弹飞 修改子弹y坐标"""
        self.y += self.speed


# 背景音乐类
class GameSonud(object):
    def __init__(self):
        pygame.mixer.init()  # 音乐模块初始化
        pygame.mixer.music.load('E:\pythonV1\python-script\item one\素材\背景音乐.wav')
        pygame.mixer.music.set_volume(0.5)  # 音量大小

    def playBackgroundMusic(self):
        pygame.mixer.music.play(-1)  # 开始播放音乐


'''完成整个程序的控制'''


def main():
    # 调用音乐播放的方法
    sound = GameSonud()
    sound.playBackgroundMusic()
    # 1创建一个窗口
    screen = pygame.display.set_mode((457, 791), 0, 32)
    # 2创建图片当做背景
    background1 = pygame.image.load('E:\pythonV1\python-script\item one\素材\背景.png')
    # 创建一个飞机的对象
    player = Aircratf(screen)
    # 创建一个敌方飞机的对象
    enemyplane = Enemyplane(screen)

    while True:
        # 3将背景贴在窗口
        screen.blit(background1, (0, 0))

        # 遍历所有事件
        for event in pygame.event.get():
            # 判断事件类型
            if event.type == QUIT:
                # 执行并退出
                pygame.quit()
                exit()
        # 执行飞机的按键监听
        player.key_control()

        # 飞机的显示
        player.display()
        # 敌方飞机的显示
        enemyplane.display()
        # 敌机自动移动
        enemyplane.auto_move()
        # 敌机自动开火
        enemyplane.auto_fire()
        # 4显示窗口内容
        pygame.display.update()
        time.sleep(0.01)


if __name__ == '__main__':
    main()
